package cn.iocoder.yudao.module.system.controller.admin.application;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.application.vo.ApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.application.vo.ApplicationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.application.vo.ApplicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.application.ApplicationDO;
import cn.iocoder.yudao.module.system.enums.application.ApplicationEnums;
import cn.iocoder.yudao.module.system.service.application.ApplicationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getPortalLoginUserId;

@Tag(name = "管理后台 - 目录审批")
@RestController
@RequestMapping("/approval")
@Validated
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    @PostMapping("/portal/saveApplication")
    @Operation(summary = "保存目录审批")
    public CommonResult<Integer> saveApplication(@Valid @RequestBody ApplicationSaveReqVO createReqVO) throws Exception {
        createReqVO.setStatus(ApplicationEnums.Status.UN_SUBMITTED.getCode());
        return success(applicationService.createApplication(createReqVO));
    }
    @PostMapping("/portal/submitApplication")
    @Operation(summary = "提交目录审批(直接提交(新增记录))")
    public CommonResult<Integer> submitApplication(@Valid @RequestBody ApplicationSaveReqVO createReqVO) throws Exception {
        createReqVO.setStatus(ApplicationEnums.Status.SUBMITTED.getCode());
        createReqVO.setCommitTime(LocalDateTime.now());
        return success(applicationService.createApplication(createReqVO));
    }

    @PostMapping("/portal/submitByStatus")
    @Operation(summary = "提交目录审批")
    public CommonResult<Boolean> submitByStatus(@Valid @RequestBody ApplicationSaveReqVO createReqVO) {
        try {
            createReqVO.setStatus(ApplicationEnums.Status.SUBMITTED.getCode());
            Boolean result = applicationService.updateApplication(createReqVO);
            return success(result);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }



    @PutMapping("/portal/recallApplication")
    @Operation(summary = "撤回目录审批")
    public CommonResult<Boolean> recallApplication(@Valid @RequestBody ApplicationSaveReqVO updateReqVO) {
        updateReqVO.setStatus(ApplicationEnums.Status.UN_SUBMITTED.getCode());
        try {
            Boolean result = applicationService.updateApplication(updateReqVO);
            return success(result);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @PutMapping("/portal/updateApplication")
    @Operation(summary = "更新目录审批")
    public CommonResult<Boolean> updateApplication(@Valid @RequestBody ApplicationSaveReqVO updateReqVO) {
        try {
            Boolean result = applicationService.updateApplication(updateReqVO);
            return success(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/portal/deleteApplication")
    @Operation(summary = "删除目录审批")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteApplication(@RequestParam("id") Integer id) {
        applicationService.deleteApplication(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    public CommonResult<Boolean> deleteApplicationList(@RequestParam("ids") List<Integer> ids) {
        applicationService.deleteApplicationListByIds(ids);
        return success(true);
    }

    @GetMapping("/portal/getApplication")
    @Operation(summary = "获得目录审批")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ApplicationRespVO> getApplication(@RequestParam("id") Integer id) {
        ApplicationDO application = applicationService.getApplication(id);
        return success(BeanUtils.toBean(application, ApplicationRespVO.class));
    }
    @GetMapping("/portal/applicationDetail")
    @Operation(summary = "获得目录审批分页")
    public CommonResult<ApplicationRespVO> getApplicationDetail(@Valid ApplicationPageReqVO pageReqVO) {
        ApplicationDO application = applicationService.getApplicationDetail(pageReqVO);
        return success(BeanUtils.toBean(application, ApplicationRespVO.class));
    }

    @GetMapping("/portal/applicationPage")
    @Operation(summary = "获得目录审批分页")
    public CommonResult<PageResult<ApplicationRespVO>> getApplicationPage(@Valid ApplicationPageReqVO pageReqVO) {
        pageReqVO.setCreateBy(String.valueOf(getPortalLoginUserId()));
        PageResult<ApplicationDO> pageResult = applicationService.getApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationRespVO.class));
    }
    @GetMapping("/applicationPage")
    @Operation(summary = "获得目录审批分页")
    public CommonResult<PageResult<ApplicationRespVO>> findApplicationPage(@Valid ApplicationPageReqVO pageReqVO) {
        PageResult<ApplicationDO> pageResult = applicationService.getApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationRespVO.class));
    }
    @GetMapping("/portal/applicationAuditPage")
    @Operation(summary = "根据登录用户获取待审核的数据")
    public CommonResult<PageResult<ApplicationRespVO>> getApplicationAuditPage(@Valid ApplicationPageReqVO pageReqVO) {
        PageResult<ApplicationDO> pageResult = applicationService.getApplicationAuditPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ApplicationRespVO.class));
    }
    @GetMapping("/portal/AuditCount")
    @Operation(summary = "获取待审核数量")
    public CommonResult<Integer> getAuditCount() {
        return success(applicationService.getAuditCount());
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出目录审批 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApplicationExcel(@Valid ApplicationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ApplicationDO> list = applicationService.getApplicationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "目录审批.xls", "数据", ApplicationRespVO.class,
                        BeanUtils.toBean(list, ApplicationRespVO.class));
    }

}